﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.UserModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmUserManage : Form
    {
        public FrmUserManage()
        {
            InitializeComponent();
        }

        UserPurviewManager userPurviewManager = new UserPurviewManager();

        void SetHeaderText()
        {
            dgrdvUser.Columns[0].HeaderText = "用户名";
            dgrdvUser.Columns[0].Width = 70;
            dgrdvUser.Columns[1].HeaderText = "用户管理";
            dgrdvUser.Columns[1].Width = 78;
            dgrdvUser.Columns[2].HeaderText = "进货管理";
            dgrdvUser.Columns[2].Width = 78;
            dgrdvUser.Columns[3].HeaderText = "商品查询";
            dgrdvUser.Columns[3].Width = 78;
            dgrdvUser.Columns[4].HeaderText = "商品销售";
            dgrdvUser.Columns[4].Width = 78;
            dgrdvUser.Columns[5].HeaderText = "销售查询";
            dgrdvUser.Columns[5].Width = 78;
            dgrdvUser.Columns[6].HeaderText = "销售统计";
            dgrdvUser.Columns[6].Width = 78;
            dgrdvUser.Columns[7].HeaderText = "库存报警";
            dgrdvUser.Columns[7].Width = 78;
            dgrdvUser.Columns[8].HeaderText = "库存查询";
            dgrdvUser.Columns[8].Width = 78;
            dgrdvUser.Columns[9].HeaderText = "数据备份";
            dgrdvUser.Columns[9].Width = 78;
            dgrdvUser.Columns[10].HeaderText = "数据恢复";
            dgrdvUser.Columns[10].Width = 78;
        }

        private void FrmUserManage_Load(object sender, EventArgs e)
        {
            try
            {
                List<UserPurview> list = userPurviewManager.GetUserPurviews();
                dgrdvUser.DataSource = list;
                SetHeaderText();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgrdvUser_CurrentCellChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgrdvUser.CurrentCell == null)
                {
                    return;
                }
                int n = dgrdvUser.CurrentCell.RowIndex;
                dgrdvUser.Rows[n].Selected = true;
                chbUserManege.Checked = dgrdvUser.Rows[n].Cells[1].Value.ToString() == "是" ? true : false;
                chbPurchaseGoods.Checked = dgrdvUser.Rows[n].Cells[2].Value.ToString() == "是" ? true : false;
                chbGoodsQuery.Checked = dgrdvUser.Rows[n].Cells[3].Value.ToString() == "是" ? true : false;
                chbGoodsSale.Checked = dgrdvUser.Rows[n].Cells[4].Value.ToString() == "是" ? true : false;
                chbSaleDataQuery.Checked = dgrdvUser.Rows[n].Cells[5].Value.ToString() == "是" ? true : false;
                chbSaleDataCount.Checked = dgrdvUser.Rows[n].Cells[6].Value.ToString() == "是" ? true : false;
                chbInventoryAlarm.Checked = dgrdvUser.Rows[n].Cells[7].Value.ToString() == "是" ? true : false;
                chbInventoryQuery.Checked = dgrdvUser.Rows[n].Cells[8].Value.ToString() == "是" ? true : false;
                chbBackupData.Checked = dgrdvUser.Rows[n].Cells[9].Value.ToString() == "是" ? true : false;
                chbRestoreData.Checked = dgrdvUser.Rows[n].Cells[10].Value.ToString() == "是" ? true : false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSetPurview_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgrdvUser.CurrentCell == null)
                {
                    MessageBox.Show("没有需要设置权限的用户", "提示");
                    return;
                }
                int n = dgrdvUser.CurrentCell.RowIndex;
                string userName = dgrdvUser.Rows[n].Cells[0].Value.ToString().Trim();
                if (UserManager.userInfo[0] == userName)
                {
                    MessageBox.Show("不能给自己设置权限", "提示");
                    dgrdvUser_CurrentCellChanged(sender, e);
                    return;
                }
                UserPurview userPurview = new UserPurview();
                userPurview.UserName = userName;
                userPurview.UserManage = chbUserManege.Checked ? "是" : "否";
                userPurview.PurchaseGoods = chbPurchaseGoods.Checked ? "是" : "否";
                userPurview.GoodsQuery = chbGoodsQuery.Checked ? "是" : "否";
                userPurview.GoodsSale = chbGoodsSale.Checked ? "是" : "否";
                userPurview.SaleDataQuery = chbSaleDataQuery.Checked ? "是" : "否";
                userPurview.SaleDataCount = chbSaleDataCount.Checked ? "是" : "否";
                userPurview.InventoryAlarm = chbInventoryAlarm.Checked ? "是" : "否";
                userPurview.InventoryQuery = chbInventoryQuery.Checked ? "是" : "否";
                userPurview.BackupData = chbBackupData.Checked ? "是" : "否";
                userPurview.RestoreData = chbRestoreData.Checked ? "是" : "否";
                if (userPurviewManager.ChangeUserPurview(userPurview))
                {
                    dgrdvUser.Rows[n].Cells[1].Value = chbUserManege.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[2].Value = chbPurchaseGoods.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[3].Value = chbGoodsQuery.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[4].Value = chbGoodsSale.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[5].Value = chbSaleDataQuery.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[6].Value = chbSaleDataCount.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[7].Value = chbInventoryAlarm.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[8].Value = chbInventoryQuery.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[9].Value = chbBackupData.Checked ? "是" : "否";
                    dgrdvUser.Rows[n].Cells[10].Value = chbRestoreData.Checked ? "是" : "否";
                    MessageBox.Show("用户 " + userName + " 的权限设置成功", "提示");
                }
                else
                {
                    MessageBox.Show("权限设置失败", "提示");
                    dgrdvUser_CurrentCellChanged(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgrdvUser.Rows.Count == 0)
                {
                    MessageBox.Show("没有可以删除的户名", "提示");
                    return;
                }
                int n = dgrdvUser.CurrentCell.RowIndex;
                string userName = dgrdvUser.Rows[n].Cells[0].Value.ToString().Trim();
                if (UserManager.userInfo[0] == userName)
                {
                    MessageBox.Show("不能删除自己", "提示");
                    return;
                }
                if (MessageBox.Show("确定要删除用户 " + userName + " 吗?", "删除用户", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    PublicMethodsManager publicMethodsManager = new PublicMethodsManager();
                    if (publicMethodsManager.RemoveUserFromThreeTable(userName))
                    {
                        List<UserPurview> list = userPurviewManager.GetUserPurviews();
                        dgrdvUser.DataSource = list;
                        if (list.Count == 0)
                        {
                            chbUserManege.Checked = false;
                            chbPurchaseGoods.Checked = false;
                            chbGoodsQuery.Checked = false;
                            chbGoodsSale.Checked = false;
                            chbSaleDataQuery.Checked = false;
                            chbSaleDataCount.Checked = false;
                            chbInventoryAlarm.Checked = false;
                            chbInventoryQuery.Checked = false;
                            chbBackupData.Checked = false;
                            chbRestoreData.Checked = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
